#import "FormUIView.h"
#import "ImageConfig.h"
#import "FontFactory.h"
#import "DictionaryUtil.h"
#import "HttpRequestParamEnginner.h"
#import "AlertViewEnginner.h"
#import "StringUtil.h"

@implementation CFormUIView
@synthesize iViewType;
@synthesize iLucktyType;
- (id)initWithFrame:(CGRect)frame;
{
	if(self==[super initWithFrame:frame])
	{
		CGRect rect;
		int labelWidth=250;
		int viewWidth=frame.size.width;
		rect.origin.x=40;
		rect.origin.y=30;
		rect.size.width=labelWidth;
		rect.size.height=20;
		self.backgroundColor=[UIColor lightGrayColor];
		UILabel *noteLabel=[[UILabel alloc] initWithFrame:rect];
		[noteLabel setBackgroundColor:[UIColor clearColor]];
		[noteLabel setFont:[CFontFactory CreateFontBySizeAndWeight:15 weightTag:1]];
		[noteLabel setText:@"第一步：选择出生日期"];
		[noteLabel setTextColor:[UIColor whiteColor]];
		//[noteLabel setTextColor:[UIColor colorWithRed:1 green:1 blue:0 alpha:1]];
		[self addSubview:noteLabel];
		[noteLabel release];
		rect.origin.y=60;
		rect.origin.x=50;
		rect.size.width=150;
		rect.size.height=30;
		
		iDateDisplay=[[UIDatetimeControl alloc] initWithFrame:rect];
		//iTimeDisplay.userInteractionEnabled=NO;
		iDateDisplay.borderStyle = UITextBorderStyleRoundedRect;
		CHttpRequestParamEnginner *enginner=[CHttpRequestParamEnginner GetIntance];
		NSString *birthday=enginner->iParam.iBirthday;
		NSRange range;
		NSRange tempRange;
		NSString *birthdayDate;
		NSString *birthdayTime;
		birthdayDate=@"";
		birthdayTime=@"";
		if([birthday length]>1)
		{
			range=[birthday rangeOfString:@" "];
			tempRange.location=0;
			tempRange.length=range.location;
			birthdayDate=[birthday substringWithRange:tempRange];
			tempRange.location=range.location+range.length-1;
			tempRange.length=[birthday length]-tempRange.location;
			birthdayTime=[birthday substringWithRange:tempRange];
		}
		iDateDisplay.text=birthdayDate;
		iDateDisplay.idtType=UIDatePickerModeDate;
		iDateDisplay.iParentView=self;
		[self release];
		[self addSubview:iDateDisplay];
		rect.origin.y=90;
		rect.size.width=170;
		rect.origin.x=40;
		noteLabel=[[UILabel alloc] initWithFrame:rect];
		[noteLabel setBackgroundColor:[UIColor clearColor]];
		[noteLabel setFont:[CFontFactory CreateFontBySizeAndWeight:15 weightTag:1]];
		[noteLabel setText:@"第二步：选择出生时间"];
		[noteLabel setTextColor:[UIColor whiteColor]];
		[self addSubview:noteLabel];
		[noteLabel release];
		rect.origin.y=120;
		rect.origin.x=50;
		rect.size.width=150;
		iTimeDisplay=[[UIDatetimeControl alloc] initWithFrame:rect];
		//iTimeDisplay.userInteractionEnabled=NO;
		iTimeDisplay.borderStyle = UITextBorderStyleRoundedRect;
		iTimeDisplay.text=birthdayTime;
		iTimeDisplay.idtType=UIDatePickerModeTime;
		iTimeDisplay.iParentView=self;
		[self release];
		[self addSubview:iTimeDisplay];
		rect.origin.y=150;
		rect.origin.x=40;
		rect.size.width=160;
		UILabel *addrLabel=[[UILabel alloc] initWithFrame:rect];
		[addrLabel setBackgroundColor:[UIColor clearColor]];
		[addrLabel setFont:[CFontFactory CreateFontBySizeAndWeight:15 weightTag:1]];
		[addrLabel setText:@"第二步：选择出生地点"];
		[addrLabel setTextColor:[UIColor whiteColor]];
		[self addSubview:addrLabel];
		[addrLabel release];
		rect.origin.x=50;
		rect.size.width=160;
		rect.origin.y=180;
		NSString *addr=@"未选择";
		if([enginner->iParam.iProvinceId length]>1)
		{
			addr=[NSString stringWithFormat:@"%@%@%@",enginner->iParam.iProvince,enginner->iParam.iCity,enginner->iParam.iZone];
		}
		iAddrDisplayLabel=[[UILabel alloc] initWithFrame:rect];
		[iAddrDisplayLabel setFont:[CFontFactory CreateFontBySizeAndWeight:12 weightTag:1]];
		[iAddrDisplayLabel setText:addr];
		[iAddrDisplayLabel setBackgroundColor:[UIColor clearColor]];
		[iAddrDisplayLabel setTextColor:[UIColor whiteColor]];
		[self addSubview:iAddrDisplayLabel];
		
		rect.origin.x=170;
		CUIButtonControl *button=[[CUIButtonControl alloc]init];
		button.iFatherHeight=30;
		button.iFatherWidth=viewWidth;
		button.iWidthPt=50;
		//[button.iText appendString:[NSMutableString stringWithString:@"选择地区"]];
		button.iText=[NSMutableString stringWithString:@"选择地区"];
		[button CustomInit];
		[button CaculateSize];
		[button AddTarget:self action:@selector(DoSelectAddr:forEvent:) forControlEvents:UIControlEventTouchUpInside];
		UILayoutView *layout=[[UILayoutView alloc] initWithFrame:rect];
		[layout addSubview:button];
		[button release];
		[self addSubview:layout];
		[layout release];
				
		return self;
	}
	return self;
}

-(void)SetLucktyType:(LucktyType)aLucktyType
{
	iLucktyType=aLucktyType;
	CGRect rect;
	int yPos=210;
	if(iLucktyType==LucktyTypeFateSY||iLucktyType==LucktyTypeFateJT||iLucktyType==LucktyTypeFateAQ||iLucktyType==LucktyTypeFateCY||iLucktyType==LucktyTypeFateHY)
	{
		
		rect.origin.x=40;
		rect.size.width=150;
		rect.origin.y=yPos;
		yPos+=30;
		rect.size.height=20;
		UILabel *noteLabel=[[UILabel alloc] initWithFrame:rect];
		[noteLabel setBackgroundColor:[UIColor clearColor]];
		[noteLabel setFont:[CFontFactory CreateFontBySizeAndWeight:15 weightTag:1]];
		[noteLabel setText:@"第三步：选择性别"];
		[noteLabel setTextColor:[UIColor whiteColor]];
		[self addSubview:noteLabel];
		[noteLabel release];
		rect.origin.x=50;
		rect.size.width=150;
		rect.origin.y=yPos;
		yPos+=30;
		iSex=[[UICustomSwitch alloc] initWithFrame:rect];
		//((UILabel *)[[[[[[iSex subviews] lastObject] subviews] objectAtIndex:2] subviews] objectAtIndex:0]).text = @"男";
		
		//((UILabel *)[[[[[[iSex subviews] lastObject] subviews] objectAtIndex:2] subviews] objectAtIndex:1]).text = @"女";
		//((UILabel *)[[[[[[isFooOrBar subviews] lastObject] subviews] objectAtIndex:2] subviews] objectAtIndex:1]).backgroundColor=[UIColor blueColor];
		[iSex setLeftLabelText:@"男"
								font:[UIFont boldSystemFontOfSize: 17.0f]
							   color:[UIColor whiteColor]];
		[iSex setRightLabelText:@"女"
								 font:[UIFont boldSystemFontOfSize: 17.0f]
								color:[UIColor grayColor]];
		CHttpRequestParamEnginner *enginner=[CHttpRequestParamEnginner GetIntance];
		if([enginner->iParam.iSex compare:@"1"]==0)
		{
			[iSex setOn:YES];
		}
		[self addSubview:iSex];
		
	}
	rect.origin.x=120;
	rect.origin.y=yPos+15;
	rect.size.width=80;
	rect.size.height=30;
	UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
	[button setTitle:@"查看结果" forState:UIControlStateNormal];
	button.frame=rect;
	[button addTarget:self action:@selector(DoGetResult:forEvent:) forControlEvents:UIControlEventTouchUpInside];
	[self addSubview:button];


	
}
-(void)DoGetResult:(id)sender forEvent:(UIEvent *)event
{
	BOOL hasSex;
	int fateType;
	int sex;
	NSString *partUrl;
	time_t currentTime;
	time(&currentTime);
	NSMutableString *allUrl;
	NSString *valueString;
	CHttpRequestParamEnginner *enginner;
	u_int32_t val =arc4random();
	hasSex=FALSE;
	if([iDateDisplay.text length]<=0)
	{
		[[CAlertViewEnginner GetIntance] Alert:@"请选择出生日期"];
		return ;
	}
	if([iTimeDisplay.text length]<=0)
	{
		[[CAlertViewEnginner GetIntance] Alert:@"请选择出生时间"];
		return ;
	}
	enginner=[CHttpRequestParamEnginner GetIntance];
	if([enginner->iParam.iZoneId length]<=1)
	{
		[[CAlertViewEnginner GetIntance] Alert:@"请选择出生地点"];
		return ;
	}

	NSString *orderNO=[NSString stringWithFormat:@"%d%d",(long)currentTime,val];
	NSString *clientNO=@"BA584060C8F2BB594CE3F17766BA76D3";
	allUrl=[[NSMutableString alloc]init];
	switch (iLucktyType) {
		case LucktyTypeWeek:
			
			partUrl=@"GetWeekLuck";
			break;
		case LucktyTypeMonth:
			
			partUrl=@"GetMonthLuck";
			break;
		case LucktyTypeTag:
			
			partUrl=@"GetTag";
			break;
		case LucktyTypeFateSY:
			partUrl=@"GetFate";
			hasSex=TRUE;
			fateType=0;
			break;
		case LucktyTypeFateJT:
			partUrl=@"GetFate";
			hasSex=TRUE;
			fateType=2;
			break;	
		case LucktyTypeFateAQ:
			partUrl=@"GetFate";
			hasSex=TRUE;
			fateType=5;
			break;
		case LucktyTypeFateCY:
			partUrl=@"GetFate";
			hasSex=TRUE;
			fateType=4;
			break;
		case LucktyTypeFateHY:
			partUrl=@"GetFate";
			hasSex=TRUE;
			fateType=6;
			break;
			
		default:
			break;
	}
	[allUrl appendFormat:@"http://soft.luckty.com/Product/%@/?clientNo=%@&orderno=",partUrl,clientNO];
	valueString = [ CStringUtil URLencode:orderNO stringEncoding:NSUTF8StringEncoding];
	[allUrl appendFormat:@"%@&birthday=",valueString];
	valueString=[NSString stringWithFormat:@"%@ %@",iDateDisplay.text,iTimeDisplay.text];
	enginner->iParam.iBirthday=valueString;
	valueString = [ CStringUtil URLencode:valueString stringEncoding:NSUTF8StringEncoding];
	[allUrl appendFormat:@"%@&place=",valueString];
	valueString = [ CStringUtil URLencode:enginner->iParam.iZoneId stringEncoding:NSUTF8StringEncoding];
	[allUrl appendFormat:@"%@",valueString];
	if(hasSex)
	{
		sex=-1;
		if(iSex.on)
		{
			sex=1;
		}
		[allUrl appendFormat:@"&sex=%d&fateType=%d",sex,fateType];
		enginner->iParam.iSex=[NSString stringWithFormat:@"%d",sex];
	}
	[allUrl appendFormat:@"&j1=0&j2=0&w1=0&w2=0"];
	NSMutableDictionary *userInfo=[[NSMutableDictionary alloc] init];
	[CDictionaryUtil SetIntValue:userInfo tagKey:@"viewType" tagIntValue:(int)ResultViewType];
	[CDictionaryUtil SetIntValue:userInfo tagKey:@"lucktyType" tagIntValue:(int)iLucktyType];
	[userInfo setObject:allUrl forKey:@"allUrl"];
	[allUrl release];
	[[NSNotificationCenter defaultCenter] postNotificationName:KNotificationSwitchView object:self userInfo:userInfo];
	[userInfo release];
	[enginner SaveHttpRequestParam];
}

-(void)HttpTextResponse:(HttpTextResponseData*)data
{
	int length;
	unsigned char *xmlString;
	length=data->length;
	xmlString=data->data;
	free(xmlString);
}
-(void)DoSelectAddr:(id)sender forEvent:(UIEvent *)event
{
	NSMutableDictionary *userInfo=[[NSMutableDictionary alloc] init];
	[CDictionaryUtil SetIntValue:userInfo tagKey:@"viewType" tagIntValue:(int)ProvinceViewType];
	[CDictionaryUtil SetIntValue:userInfo tagKey:@"lucktyType" tagIntValue:(int)iLucktyType];
	[[NSNotificationCenter defaultCenter] postNotificationName:KNotificationSwitchView object:self userInfo:userInfo];
	[userInfo release];
}
-(void)UpdateView
{
	CHttpRequestParamEnginner *enginner=[CHttpRequestParamEnginner GetIntance];
	NSString *addr=[NSString stringWithFormat:@"%@%@%@",enginner->iParam.iProvince,enginner->iParam.iCity,enginner->iParam.iZone]; 
	[iAddrDisplayLabel setText:addr];
}
-(void)dealloc
{
	if(iBackGroupImg)
	{
		[iBackGroupImg release];
	}
	if(iAddrDisplayLabel)
	{
		[iAddrDisplayLabel release];
	}
	if(iTimeDisplay)
	{
		[iTimeDisplay release];
	}
	if(iDateDisplay)
	{
		[iDateDisplay release];
	}
	if(iSex)
	{
		[iSex release];
	}
	[super dealloc];
}

@end
